package cn.zane.gateway.session.defaults;

import cn.zane.gateway.datasource.DataSource;
import cn.zane.gateway.datasource.unpooled.UnPooledDataSourceFactory;
import cn.zane.gateway.executor.Executor;
import cn.zane.gateway.session.Configuration;
import cn.zane.gateway.session.GatewaySession;
import cn.zane.gateway.session.GatewaySessionFactory;

/**
 * 泛化调用会话工厂
 *
 * @author zane
 * @since 2024-09-06
 */
public class DefaultGatewaySessionFactory implements GatewaySessionFactory {

    private final Configuration configuration;

    public DefaultGatewaySessionFactory(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public GatewaySession openSession(String uri) {
        // 获取数据源连接信息：这里把 Dubbo、HTTP 抽象为一种连接资源
        UnPooledDataSourceFactory dataSourceFactory = new UnPooledDataSourceFactory();
        dataSourceFactory.setProperties(configuration, uri);
        DataSource dataSource = dataSourceFactory.getDataSource();
        // 创建执行器
        Executor executor = configuration.newExecutor(dataSource.getConnection());
        return new DefaultGatewaySession(configuration, uri, executor);
    }
}
